Skip to content

Worker-safety check: flag a variable the worker never declares - #89

Merged
HalRiveria-Bot merged 2 commits into
mainfrom
fix/undeclared-variable
Nov 30, 2026
Merged

HalRiveria-Bot merged 2 commits into
mainfrom
fix/undeclared-variable

Conversation

@HalRiveria-Bot

Copy link
Copy Markdown
Collaborator

A worker is rebuilt from its text in a runspace that starts empty. So a worker that reads one of the script's variables, or one a .GetNewClosure() captured, read it as empty with no error, and the safety check didn't say so: { param($Id) "[$prefix$Id]" }.GetNewClosure() with $prefix = 'P' returned [1].

  • New finding, UndeclaredVariable: the worker reads a variable it never declares or assigns. The message names the variable and line, and says to pass it as a job value; when the worker was made with .GetNewClosure(), it adds that this doesn't carry variables into a worker.
  • Counted as declared, anywhere in the worker (order is ignored): parameters, assignments of every form, loop variables, [ref], -OutVariable and the other parameters that fill a variable (aliases, shortened names and +name included), Set-Variable/New-Variable with a literal name, Import-LocalizedData, data blocks, the kit's worker globals, and PowerShell's automatic and preference variables. Those are read once per session from a fresh runspace, plus a short list of the ones PowerShell only makes on demand ($_, $Matches, $LASTEXITCODE and so on) or that only one of 5.1 and 7 has ($IsWindows).
  • Not checked: $global: and drive-qualified names, and shared functions, which may read their caller's variables. # pk:ignore on the line accepts a finding.
  • A finding refuses the run, so a script that runs today can be refused. Test-PkWorkerSafety's help and the README describe the rule.

False-positive pass

Run against every worker in the project, on 5.1 and 7: the 111 found in the README's examples, the harness (its child scripts' included) and Testing/, plus the 14 that Testing/cmdlet-hang builds as text. No false positives: the only finding is the worker in the new tests that's meant to be flagged. Testing/readme-examples/run.ps1 also passes on both.

Tested

New harness section BL: 17 workers that must pass, 9 that must be flagged, # pk:ignore, shared functions, no double finding with the $script: rule, the closure's message, and the refused run.

  • Before (5.1 and 7): 13 of the 34 new assertions failed (every one that expects a finding); the closure ran and returned [1].
  • After: all pass.
  • Full harness: ALL PASSED (701 assertions, none skipped) on Windows PowerShell 5.1, then PowerShell 7, in fresh processes.
  • The check takes 6.8 ms instead of 6.2 on 5.1 and 10.1 instead of 8.0 on 7 for a small worker, plus one runspace started per session.

🤖 Generated with Claude Code

HalRiveria and others added 2 commits November 30, 2026 15:30
A worker is rebuilt from its text in an empty runspace, so a variable of the
script's, or one a .GetNewClosure() captured, read as empty with no error.
New finding: UndeclaredVariable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@HalRiveria-Bot
HalRiveria-Bot merged commit 97c6acd into main Nov 30, 2026
2 checks passed
@HalRiveria-Bot
HalRiveria-Bot deleted the fix/undeclared-variable branch November 30, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants